package com.example.clock.util;

/**
 * @author : lichengqian
 * 2024-08-07  19:48
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class WorkDayCalculator {

    // 定义工作日，通常周一至周五为工作日，可根据实际需求修改
    private static final int[] WORK_DAYS = {Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY};

    public static int countWorkDays(LocalDate startDateStr, LocalDate endDateStr) throws ParseException{
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return countWorkDays(formatter.format(startDateStr),formatter.format(endDateStr));
    }
    public static int countWorkDays(String startDateStr, String endDateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(startDateStr);
        Date endDate = sdf.parse(endDateStr);

        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startDate);

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);

        int workDayCount = 0;

        while (!startCal.after(endCal)) {
            int dayOfWeek = startCal.get(Calendar.DAY_OF_WEEK);
            for (int workDay : WORK_DAYS) {
                if (dayOfWeek == workDay) {
                    workDayCount++;
                    break;
                }
            }
            startCal.add(Calendar.DATE, 1);
        }

        return workDayCount;
    }
}